from collections import defaultdict
import logging
import jsonpickle
from BuildingType import BuildingType

FORMAT = "%(asctime)-15s %(message)s - %(filename)s %(lineno)d "
logging.basicConfig(level=logging.DEBUG, format=FORMAT)

class Player:
  def __init__(self, token):
    self.token = token
    self.buildings = []
    self.roads = []
    self.resources = defaultdict(lambda: 0)
    self.update_resources(self.resources)
    self.points = 0
  
  def summary(self):
    summary = "i:%s, p:%d, ro:%d, b:%d, re:%d"\
    %(self.token,self.points, 
     len(self.roads), len(self.buildings), sum(self.resources.values()))
    return summary
      
        
  def update_resources(self, resources):
#    logging.error("b: %s, add: %s" % (self.resources, resources))
    for type, value in resources.items():
      self.resources[type] += value
#    logging.error("resources %s" % self.resources)
      
  def get_points(self):
    return self.points

  def update_points(self, l_road = False):
    self.points = len(self.buildings) + len([building for building in self.buildings if building.type == BuildingType.City])
    if l_road:
      self.points += 2
    
  #Item Controllers
  def __repr__(self):
    string = ""
    string += "token=[" + str(self.token) + "],"
    string += "buildings=[" + str(["%d, %d" % (building.type, building.vertex) for building in self.buildings]) + "],"
    string += "roads=[" + str(self.roads) + "],"
    string += "resources=[" + str(self.resources) + "],"
    string += "points=[" + str(self.points) + "]\n"
    return string
  
if __name__ == "__main__":
  p = Player("bla")
  p.update_resources({"Grain":1})
  p.update_resources({"Grain":1})
  p.update_resources({"Grain":1})
  p.update_resources({"Brick":1})
  print p.summary()